<%@page import="monster.comm.util.SpringApplicationContextFactory"%>
<%@page import="monster.flight.admin.manager.service.admin.Flight_infoService"%>
<%@page import="monster.flight.comm.util.FlightFactory"%>
<%@page import="monster.comm.util.DateUtil"%>
<%@page import="monster.flight.comm.entity.vo.FlightBay"%>
<%@page import="sun.jdbc.rowset.CachedRowSet"%>
<%@page import="monster.flight.comm.entity.vo.Flight_info"%>
<%@page import="monster.comm.database.DBManager"%>
<%@page import="monster.comm.util.SysConstant"%>
<%@page import="monster.comm.util.UserService"%>
<%@page import="monster.comm.util.Tool"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String now_time=Tool.getRequest("now_time",request);//必传参数
String list="";

if(now_time.length()==0){
	out.println("{\"id\":-101,\"now_time\":\"你还没有选择时间！\"}");
	return;
}

String flight_date = DateUtil.getDate("yyyy-MM-dd");
/*
if(!now_time.equals(flight_date)){
	Flight_infoService  flight_infoService =(Flight_infoService) SpringApplicationContextFactory.getService("flight_infoService");
	flight_infoService.fill(now_time);
	FlightFactory.getInstatnce().addFlight(now_time);
}
**/
//UserService user = (UserService) session.getAttribute(SysConstant.SESSION_NAME);//获得session里面的登陆信息
if(1==2){
	out.println("{\"id\":-100,\"name\":\"没有登录\"}");
	return;
}else{
	try{
		String nowTime = DateUtil.getDateTime("yyyy-MM-dd HH:mm:ss",new Date());
		StringBuffer result = new StringBuffer(1000);
		Map<String,FlightBay> bay_map= new TreeMap<String,FlightBay> ();//飞机港位
		Map<String,String> flight_staff= new TreeMap<String,String> ();//飞机维修人员
		DBManager db = new DBManager();
		String sql = "select a.*,b.AIRPORT_ID,b.BAY_POSITION_CSS from FLIGHT_INFO a inner join FLIGHT_AIRPORT b on a.bay=b.bay "
					+"   WHERE a.BAY IS not NULL and a.BAY !='' and flight_date like '"+now_time+"%' "
					//+"  and STD >='"+curTime+"'"+" and ETA>='"+curTime+"'"
					+"  ORDER BY BAY desc";
		CachedRowSet cr = db.getRowSet(sql);
		//存飞机港位
		while(cr.next()){
			String BAY=Tool.getRsValue(cr,"BAY");//出港机位
			if(bay_map.get(BAY)==null){
				String Airport_id=Tool.getRsValue(cr,"Airport_id");
				String Bay_postion_css=Tool.getRsValue(cr,"BAY_POSITION_CSS");
				Map<String,Flight_info> in_map= new TreeMap<String,Flight_info> ();//飞机港位
				Map<String,Flight_info> out_map= new TreeMap<String,Flight_info> ();//飞机港位
				FlightBay f_bay= new FlightBay();
				f_bay.in_map=in_map;
				f_bay.out_map=out_map;
				f_bay.bay=BAY;
				f_bay.setAirport_id(Airport_id);
				f_bay.setBay_css(Bay_postion_css);
				bay_map.put(BAY,f_bay);
			}
		}
		Map<String,String> pmap = new HashMap<String,String>();
		pmap.put("1","机");
		pmap.put("2","放");
		pmap.put("3","上");
		pmap.put("4","学");
		//查询航班维修人员
		sql = "select a.flight_id,b.staff_name,c.position from FLIGHT_INFO a inner join PLAN_INFO b on a.flight_id=b.flight_id "
					+" join STAFF_INFO c on b.staff_sn=c.staff_sn "
					+"   WHERE a.BAY IS not NULL and a.BAY !='' and a.flight_date like '"+now_time+"%' "
					+"   and a.ARRIVAL_AIRPORT='ZUUU'  ";
					
		cr = db.getRowSet(sql); 
		while(cr.next()){
			String flight_id  =Tool.getRsValue(cr,"flight_id");
			String staff_name  =Tool.getRsValue(cr,"staff_name");
			String position = Tool.getRsValue(cr,"position");
			position= pmap.get(position);
			if(!flight_staff.containsKey(flight_id)){
				flight_staff.put(flight_id,position+":"+staff_name);
			}else{
				String na=flight_staff.get(flight_id);
				flight_staff.put(flight_id,na+"  "+position+":"+staff_name);
			}
		}
		//存进港飞机 最新数据
		
		sql = "select a.*,b.AIRPORT_ID,b.BAY_POSITION_CSS from FLIGHT_INFO a inner join FLIGHT_AIRPORT b on a.bay=b.bay "
					
					+"   WHERE a.BAY IS not NULL and a.BAY !='' and flight_date like '"+now_time+"%' "
					+"   and ARRIVAL_AIRPORT='ZUUU'  "
					+"   ORDER BY BAY desc";
		cr = db.getRowSet(sql); 
		while(cr.next()){
			Flight_info vo = new Flight_info();
			String FLIGHT_ID=Tool.getRsValue(cr,"FLIGHT_ID");vo.setFlight_id(FLIGHT_ID);
			String FLIGHT_DATE=Tool.getRsValue(cr,"FLIGHT_DATE");vo.setFlight_date(FLIGHT_DATE);
			String FLIGHT_NO=Tool.isNull(Tool.getRsValue(cr,"FLIGHT_NO"));vo.setFlight_no(FLIGHT_NO);
			String AC_TYPE=Tool.isNull(Tool.getRsValue(cr,"AC_TYPE"));vo.setAc_type(AC_TYPE);
			String AC_REG=Tool.isNull(Tool.getRsValue(cr,"AC_REG"));vo.setAc_reg(AC_REG);
			String DEPARTURE_AIRPORT=Tool.isNull(Tool.getRsValue(cr,"DEPARTURE_AIRPORT"));vo.setDeparture_airport(DEPARTURE_AIRPORT);
			String ARRIVAL_AIRPORT=Tool.isNull(Tool.getRsValue(cr,"ARRIVAL_AIRPORT"));vo.setArrival_airport(ARRIVAL_AIRPORT);
			String STD=Tool.isNull(Tool.getRsValue(cr,"STD"));vo.setStd(STD);//计划起飞时间
			String ETD=Tool.isNull(Tool.getRsValue(cr,"ETD"));vo.setEtd(ETD);//预计起飞时间
			String STA=Tool.isNull(Tool.getRsValue(cr,"STA"));vo.setSta(STA);//计划到达时间
			String ETA=Tool.isNull(Tool.getRsValue(cr,"ETA"));vo.setEta(ETA);//预计到达时间
			String ATA=Tool.isNull(Tool.getRsValue(cr,"ATA"));vo.setAta(ATA);//实际到达时间
			String BAY=Tool.isNull(Tool.getRsValue(cr,"BAY"));vo.setBay(BAY);//出港机位
			String CREATE_TIME=Tool.isNull(Tool.getRsValue(cr,"CREATE_TIME"));vo.setCreate_time(CREATE_TIME);//出港机位
			String na = flight_staff.get(FLIGHT_ID);
			vo.setStaff(na==null?"":na);
			String Airport_id=Tool.isNull(Tool.getRsValue(cr,"Airport_id"));vo.setAirport_id(Airport_id);
			String Bay_postion_css=Tool.isNull(Tool.getRsValue(cr,"BAY_POSITION_CSS"));vo.setBay_postion_css(Bay_postion_css);
			if("ZUUU".equals(ARRIVAL_AIRPORT)){//到达站为成都
				FlightBay f_bay=bay_map.get(BAY);//得到机位
				Map<String,Flight_info> inMap=f_bay.getIn_map();//得到 存入了的到达站为成都数据
				
				if(!"".equals(ETA)){//预计到达时间不为空
					if(inMap.size()==0){
						inMap.put("1",vo);
					}else{
						Flight_info old_vo = inMap.get("1");
						ATA = old_vo.getAta();
						String ata = vo.getAta();
						if(ATA.equals("")&&ata.equals("")){//都没有到达比较预计到达时间
							Date eta_time = DateUtil.convertStringToDate("yyyy-MM-dd HH:mm:ss",ETA);
							Date old_eta_time = DateUtil.convertStringToDate("yyyy-MM-dd HH:mm:ss",old_vo.getEta());
							if(eta_time.before(old_eta_time)){//eta_time时间在old_eta_time之前，那更新Inmap 要存如离当前时间最远的数据
								inMap.put("1",vo);
							}
						}else if(!ATA.equals("")&&ata.equals("")){//之前的已经到达则取本次
							inMap.put("1",vo);
						}else if(ATA.equals("")&&!ata.equals("")){//本次的已经到达则取之前
							
						}else if(!ATA.equals("")&&!ata.equals("")){//都已经到达 比较实际到达时间
							Date ata_time = DateUtil.convertStringToDate("yyyy-MM-dd HH:mm:ss",ATA);
							Date old_ata_time = DateUtil.convertStringToDate("yyyy-MM-dd HH:mm:ss",old_vo.getAta());
							if(ata_time.after(old_ata_time)){//ata_time时间在old_ata_time之前，那更新Inmap 要存如离当前时间最近的数据
								inMap.put("1",vo);
							}
						}
						
					}
					 
				}
				
			}
		
		}
		//查询航班维修人员
		sql = "select a.flight_id,b.staff_name,c.position from FLIGHT_INFO a inner join PLAN_INFO b on a.flight_id=b.flight_id "
			+" join STAFF_INFO c on b.staff_sn=c.staff_sn "
			+"   WHERE a.BAY IS not NULL and a.BAY !='' and a.flight_date like '"+now_time+"%' "
			+"  and (a.ATD IS  NULL OR A.ATD='')   and a.DEPARTURE_AIRPORT='ZUUU' ";
			
		cr = db.getRowSet(sql); 
		while(cr.next()){
			String flight_id  =Tool.getRsValue(cr,"flight_id");
			String staff_name  =Tool.getRsValue(cr,"staff_name");
			String position = Tool.getRsValue(cr,"position");
			position= pmap.get(position);
			if(!flight_staff.containsKey(flight_id)){
				flight_staff.put(flight_id,position+":"+staff_name);
			}else{
				String na=flight_staff.get(flight_id);
				flight_staff.put(flight_id,na+"  "+position+":"+staff_name);
			}
		}
		//存出港飞机 最新数据
		sql = "select a.*,b.AIRPORT_ID,b.BAY_POSITION_CSS,d.airport_ch_name from FLIGHT_INFO a inner join FLIGHT_AIRPORT b on a.bay=b.bay "
					+" join airport_city d on a.ARRIVAL_AIRPORT=d.airport_code  WHERE a.BAY IS not NULL and a.BAY !='' and flight_date like '"+now_time+"%' "
					+" and (a.ATD IS  NULL OR A.ATD='')   and DEPARTURE_AIRPORT='ZUUU' "
					+"  ORDER BY BAY desc";
		cr = db.getRowSet(sql); 
		while(cr.next()){
			Flight_info vo = new Flight_info();
			String FLIGHT_ID=Tool.getRsValue(cr,"FLIGHT_ID");vo.setFlight_id(FLIGHT_ID);
			String FLIGHT_DATE=Tool.getRsValue(cr,"FLIGHT_DATE");vo.setFlight_date(FLIGHT_DATE);
			String FLIGHT_NO=Tool.isNull(Tool.getRsValue(cr,"FLIGHT_NO"));vo.setFlight_no(FLIGHT_NO);
			String AC_TYPE=Tool.isNull(Tool.getRsValue(cr,"AC_TYPE"));vo.setAc_type(AC_TYPE);
			String AC_REG=Tool.isNull(Tool.getRsValue(cr,"AC_REG"));vo.setAc_reg(AC_REG);
			String DEPARTURE_AIRPORT=Tool.isNull(Tool.getRsValue(cr,"DEPARTURE_AIRPORT"));vo.setDeparture_airport(DEPARTURE_AIRPORT);
			String ARRIVAL_AIRPORT=Tool.isNull(Tool.getRsValue(cr,"airport_ch_name"));vo.setArrival_airport(ARRIVAL_AIRPORT);
			String STD=Tool.isNull(Tool.getRsValue(cr,"STD"));vo.setStd(STD);//计划起飞时间
			String ETD=Tool.isNull(Tool.getRsValue(cr,"ETD"));vo.setEtd(ETD);//预计起飞时间
			String STA=Tool.isNull(Tool.getRsValue(cr,"STA"));vo.setSta(STA);//计划到达时间
			String ETA=Tool.isNull(Tool.getRsValue(cr,"ETA"));vo.setEta(ETA);//预计到达时间
			String BAY=Tool.isNull(Tool.getRsValue(cr,"BAY"));vo.setBay(BAY);//出港机位
			String CREATE_TIME=Tool.isNull(Tool.getRsValue(cr,"CREATE_TIME"));vo.setCreate_time(CREATE_TIME);//出港机位
			String Airport_id=Tool.isNull(Tool.getRsValue(cr,"Airport_id"));vo.setAirport_id(Airport_id);
			String Bay_postion_css=Tool.isNull(Tool.getRsValue(cr,"BAY_POSITION_CSS"));vo.setBay_postion_css(Bay_postion_css);
			String na = flight_staff.get(FLIGHT_ID);
			vo.setStaff(na==null?"":na);
			if("ZUUU".equals(DEPARTURE_AIRPORT)){//起飞站为成都
				FlightBay f_bay=bay_map.get(BAY);//得到机位
				Map<String,Flight_info> outMap=f_bay.getOut_map();//得到 存入了的起飞站为成都数据				
				if(!"".equals(STD)){//计划起飞时间不为空
					if(outMap.size()==0){
						outMap.put("1",vo);
					}else{
						Flight_info old_vo = outMap.get("1");
						Date std_time = DateUtil.convertStringToDate("yyyy-MM-dd HH:mm:ss",STD);
						Date old_std_time = DateUtil.convertStringToDate("yyyy-MM-dd HH:mm:ss",old_vo.getStd());
						if(std_time.before(old_std_time)){//取同机位上时间最小的
							outMap.put("1",vo);
						}
					}					
				}
				
			}
			
		}
		
		//构建显示JOSN数据
		//[{},{}]第一层为机位
		//[{机位：201,inflight:{},outflight:{}}]第二层为进出港数据
		result.delete(0,result.length());
		Iterator<FlightBay> bayIter = bay_map.values().iterator();
		
		int i=0;
		while(bayIter.hasNext()){
			FlightBay vo =	bayIter.next();
			Map<String,Flight_info> inMap = vo.getIn_map();//达站为成都			
			result.append("{").append("\"bay\":\"").append(vo.bay).append("\"");
			result.append(",\"BAY_POSITION_CSS\":\"").append(vo.getBay_css()).append("\"");
			result.append(",\"AIRPORT_ID\":\"").append(vo.getAirport_id()).append("\"");
			if(inMap!=null&&inMap.size()>0){
				Flight_info in_flight= inMap.get("1");//到达站为成都的飞机最新数据
				result.append(",\"inflight\":{");
				result.append("\"FLIGHT_ID\":\"").append(in_flight.getFlight_id()).append("\",");
				result.append("\"FLIGHT_DATE\":\"").append(in_flight.getFlight_date()).append("\",");
				result.append("\"FLIGHT_NO\":\"").append(in_flight.getFlight_no()).append("\",");
				result.append("\"AC_TYPE\":\"").append(in_flight.getAc_type()).append("\",");
				result.append("\"AC_REG\":\"").append(in_flight.getAc_reg()).append("\",");
				result.append("\"DEPARTURE_AIRPORT\":\"").append(in_flight.getDeparture_airport()).append("\",");
				result.append("\"ARRIVAL_AIRPORT\":\"").append(in_flight.getArrival_airport()).append("\",");
				result.append("\"STD\":\"").append(in_flight.getStd()).append("\",");
				result.append("\"ETD\":\"").append(in_flight.getEtd()).append("\",");
				result.append("\"STA\":\"").append(in_flight.getSta()).append("\",");
				result.append("\"ETA\":\"").append(in_flight.getEta()).append("\",");
				result.append("\"BAY\":\"").append(in_flight.getBay()).append("\",");
				result.append("\"STAFF\":\"").append(in_flight.getStaff()).append("\",");
				result.append("\"AIRPORT_ID\":\"").append(in_flight.getAirport_id()).append("\",");
				result.append("\"BAY_POSITION_CSS\":\"").append(in_flight.getBay_postion_css()).append("\",");
				result.append("\"CREATE_TIME\":\"").append(in_flight.getCreate_time()).append("\"");
				result.append("}");				
			}else{
				result.append(",\"inflight\":{");
				result.append("\"FLIGHT_ID\":\"-1\"");//目前本机港没有到达站为成都的数据
				result.append("}");	
			}
			Map<String,Flight_info> outMap = vo.getOut_map();//起飞站为成都
			if(outMap!=null&&outMap.size()>0){
				Flight_info out_flight= outMap.get("1");//起飞站为成都的飞机最新数据
				result.append(",\"outflight\":{");
				result.append("\"FLIGHT_ID\":\"").append(out_flight.getFlight_id()).append("\",");
				result.append("\"FLIGHT_DATE\":\"").append(out_flight.getFlight_date()).append("\",");
				result.append("\"FLIGHT_NO\":\"").append(out_flight.getFlight_no()).append("\",");
				result.append("\"AC_TYPE\":\"").append(out_flight.getAc_type()).append("\",");
				result.append("\"AC_REG\":\"").append(out_flight.getAc_reg()).append("\",");
				result.append("\"DEPARTURE_AIRPORT\":\"").append(out_flight.getDeparture_airport()).append("\",");
				result.append("\"ARRIVAL_AIRPORT\":\"").append(out_flight.getArrival_airport()).append("\",");
				result.append("\"STD\":\"").append(out_flight.getStd()).append("\",");
				result.append("\"ETD\":\"").append(out_flight.getEtd()).append("\",");
				result.append("\"STA\":\"").append(out_flight.getSta()).append("\",");
				result.append("\"ETA\":\"").append(out_flight.getEta()).append("\",");
				result.append("\"BAY\":\"").append(out_flight.getBay()).append("\",");
				result.append("\"STAFF\":\"").append(out_flight.getStaff()).append("\",");
				result.append("\"AIRPORT_ID\":\"").append(out_flight.getAirport_id()).append("\",");
				result.append("\"BAY_POSITION_CSS\":\"").append(out_flight.getBay_postion_css()).append("\",");
				result.append("\"CREATE_TIME\":\"").append(out_flight.getCreate_time()).append("\"");
				result.append("}");
			}else{
				result.append(",\"outflight\":{");
				result.append("\"FLIGHT_ID\":\"-1\"");//目前本机港没有起飞站为成都的数据
				result.append("}");	
			}
			
			if(i<bay_map.size()-1){
				result.append("},");
			}else{
				result.append("}");
			}
			i++; 
		}
		
		 list="{\"id\":100,\"flightdata\":["+result.toString()+"]}";
	}catch(Exception e){
		 e.printStackTrace();
		 list="{\"id\":-200,\"name\":\"程序异常\"}";
	}
	
	out.println(list);

}
%>
